﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using Business = SoTaiChinh.Library.Business;
using Entity = SoTaiChinh.Library.Entity;

public partial class controls_admin_listbanner : System.Web.UI.UserControl
{
    //Khởi tạo giá trị ban đầu cho cột STT trong bảng danh sách thành viên
    public int stt = 1;

    DataTable dsViTriBanner = new DataTable();
    DataTable dsBanner = new DataTable();

    Business.ViTriBanner busVitriBanner = new Business.ViTriBanner();
    Entity.ViTriBanner entViTriBanner = new Entity.ViTriBanner();
    Business.Banner busBanner = new Business.Banner();
    Entity.Banner enBanner = new Entity.Banner();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadViTriBanner();
            LoadBannerQuangCao();
        }
    }

    //Hàm load tất cả vị trí banner
    protected void LoadViTriBanner()
    {
        try
        {
            dsViTriBanner = busVitriBanner.Select();
            rptDanhSachViTriBanner.DataSource = dsViTriBanner;
            rptDanhSachViTriBanner.DataBind();
            rptHienThiTheoViTri.DataSource = dsViTriBanner;
            rptHienThiTheoViTri.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


    // Hàm load banner quảng cáo
    protected void LoadBannerQuangCao()
    {
        try
        {
            dsBanner = busBanner.Select();
            rptDanhSachBanner.DataSource = dsBanner;
            rptDanhSachBanner.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/admin/addbanner.aspx?location=yes&action=add&info=0");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptDanhSachViTriBanner_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "delete")
            {
                bool delete = false;                
                delete = DeleteFK(Convert.ToInt32(e.CommandArgument.ToString()));
                if (delete == true)
                {
                    Alert.Show("Xóa vị trí banner - quảng cáo thành công!");
                    LoadViTriBanner();
                    LoadBannerQuangCao();
                }
                else
                {
                    Alert.Show("Xóa vị trí banner - quảng cáo không thành công!");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    //Hàm xóa khóa ngoại
    protected bool DeleteFK(int id)
    {
        bool delete = false;
        try
        {
            entViTriBanner.Id = id;
            enBanner.ViTri = id;
            DataTable dtBannerByViTri = busBanner.SelectByViTriBanner(enBanner);
            if (dtBannerByViTri.Rows.Count > 0)
            {
                for (int i = 0; i < dtBannerByViTri.Rows.Count; i++)
                {
                    enBanner.Id = Convert.ToInt32(dtBannerByViTri.Rows[i]["Id"].ToString());
                    File.Delete(Server.MapPath("~/uploads/BannerQuangCao/" + dtBannerByViTri.Rows[i]["HinhAnh"].ToString()));
                    delete = busBanner.Delete(enBanner);
                }
                delete = busVitriBanner.Delete(entViTriBanner);
            }
            else
            {
                delete = busVitriBanner.Delete(entViTriBanner);
            }            
        }
        catch(Exception ex)
        {
            delete = false;
            throw ex;
        }
        return delete;
    }
    protected void btnThemBanner_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/addbanner.aspx?location=no&action=add&info=0");
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        try 
        {
            foreach (RepeaterItem liItem in rptDanhSachViTriBanner.Items)
            {
                CheckBox cbViTriBanner = liItem.FindControl("CheckSelect") as CheckBox;
                //Nếu trạng thái của checkbox = checked
                if (cbViTriBanner.Checked)
                {
                    int liId =  Convert.ToInt32((liItem.FindControl("ltId") as Literal).Text);
                    bool delete = false;
                    delete = DeleteFK(liId);
                    if (delete == true)
                    {
                        Alert.Show("Xóa vị trí banner - quảng cáo thành công!");                        
                    }
                    else
                    {
                        Alert.Show("Xóa vị trí banner - quảng cáo không thành công!");
                    }
                }
            }
            LoadViTriBanner();
            LoadBannerQuangCao();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnLamMoi_Click(object sender, EventArgs e)
    {
        LoadViTriBanner();
    }
    protected void btnLamMoiBanner_Click(object sender, EventArgs e)
    {
        LoadBannerQuangCao();
    }
    protected void rptDanhSachBanner_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "delete")
            {
                bool delete = false;
                enBanner.Id = Convert.ToInt32(e.CommandArgument.ToString());
                DataTable dt = busBanner.SelectPrimaryKey(enBanner);
                delete = busBanner.Delete(enBanner);
                if (delete == true)
                {
                    File.Delete(Server.MapPath("~/uploads/BannerQuangCao/" + dt.Rows[0]["HinhAnh"].ToString()));
                    Alert.Show("Xóa Banner - quảng cáo thành công!");   
                    LoadViTriBanner();
                    LoadBannerQuangCao();
                }
                else
                {
                    Alert.Show("Xóa Banner - quảng cáo không thành công!");
                }                
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptHienThiTheoViTri_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "hienthi")
            {
                enBanner.ViTri = Convert.ToInt32(e.CommandArgument.ToString());
                DataTable dt = busBanner.SelectByViTriBanner(enBanner);
                rptDanhSachBanner.DataSource = dt;
                rptDanhSachBanner.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void lbtTatCa_Click(object sender, EventArgs e)
    {
        try
        {
            LoadBannerQuangCao();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
